const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Cleanwebpackplugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: {
    app: './js/app.js',
    // main: './js/main.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js?v=' + new Date().getTime(),
    // publicPath: './'
  },
  module: {
    rules: [{
        test: /\.art$/,
        use: [{
          loader: 'art-template-loader',
          options: {
            imports: require.resolve('art-template/lib/runtime')
          }
        }]
      },
      {
        test: /\.tpl$/,
        loader: 'ejs-loader'
      }, {
        test: /\.(less|css)$/,
        use: ExtractTextPlugin.extract({
          use: ['css-loader', 'less-loader'],
          fallback: 'style-loader'
        })
      }, {
        test: /\.(png|jpg|gif)$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 8192,
            name: 'img/[name].[ext]?v=' + new Date().getTime()
          }
        }]
      }
    ]
  },
  optimization: {
    splitChunks: {
      chunks: 'initial',
      minSize: 30000,
      minChunks: 1,
      name: true,
      cacheGroups: {
        node_modules: {
          test: /node_modules/,
          name: "node_modules",
          priority: 10,
          enforce: true
        },
        commons: {
          test: /js/,
          name: 'commons'
        },
      }
    },
    // runtimeChunk: {
    //   name: 'page/manifest'
    // }
  },
  devServer: {
    contentBase: path.resolve(__dirname, 'dist'),
    compress: true,
    port: 9000,
    // open: true,
    inline: true,
    hot: true,
    overlay: {
      warnings: true,
      errors: true
    }
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'index.html',
      filename: 'index.html',
      // chunks: ['app', 'node_modules']
    }),
    new webpack.HotModuleReplacementPlugin(),
    new Cleanwebpackplugin(['dist']),
    new CopyWebpackPlugin([{
      from: 'lib/**/*',
      to: path.resolve(__dirname, 'dist')
    }, {
      from: 'css/**/*',
      to: path.resolve(__dirname, 'dist')
    }]),
    new ExtractTextPlugin({
      filename: 'index.css',
      disable: false,
      allChunks: true,
    }),
  ]

}